<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/common/base.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="${ctsCss}/table.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
$(document).ready(function(){//初始化
	getAllTemplateName();
 });
</script>
</head>
<%@ include file="/common/message-save1.jsp" %>
<body>
	<form id="cellValidation-form" class="form-horizontal" action="${ctx}/reportTemplateValidationController/saveRowValidation" method="post">
		<input type="hidden" id="templateId" name="templateId" value="${templateId}"/>
		<input type="hidden" name="validationType" value="4"/>
		<input type="hidden" id="annualId" name="annualId" value="${annualId}"/>
		<table>
			 <tr class="tab_grey">
			      <td width="30%">公式左边：</td>
			      <td width="70%" colspan="3">
			      		<input type="text" id="validationFormulaLeftText" />
			      </td>
		     </tr>
		     <tr class="tab_grey">
			      <td width="30%">公式符号：</td>
			      <td width="70%" colspan="3">
			      		<select id="valudationFormulaSymbol">
        					<option value="1">=</option>
        					<option value="2">></option>
        					<option value="3"><</option>
        					<option value="4">>=</option>
        					<option value="5"><=</option>
        				 </select>
			      </td>
		     </tr>
		     <tr class="tab_grey">
			      <td width="30%">关系：</td>
			      <td width="70%" colspan="3">
			      		<select name="relationSelectName" id="relationSelectName">
					    	<option value="0">请选择</option>
					    	<option value="1">相加</option>
					    	<option value="2">分别</option>
				    	</select>
			      </td>
		     </tr>
		</table>
		<table id="biaojianValidationTable">
			<tr class="tab_grey">
			      <td width="30%">所属报表</td>
			      <td width="40%">表达式</td>
			      <td width="30%">操作</td>
		    </tr>
		</table>
		<div  style="text-align: center;">
			<button type="button" class="btn btn-primary" id="addrightRptValidationFormula" onclick="addBiaojianJiaoheRight();">增加右侧公式</button>
			<button type="button" class="btn btn-primary" id="saveBtn" onclick="createTableValidationFormula();">保存</button>
		</div>
	</form>
	
</body>

<script type="text/javascript">
//存放表间校核的MAP
var tableValidationMap = new Map();


function handleDataBiaojianJiaoheRight(){
	var templateSelect = "<select name='templateSelectName'>"+allBiaojianValidationTr+"</select>";
	var expressionText="<input name='expressionTextName' type='text' value=''/>"
	var tr = '<tr>';
    tr += '<td align="center" valign="middle" class="tab_grey">' + templateSelect + '</td>';  
    tr += '<td align="center" valign="middle" class="tab_grey" >' + expressionText + '</td>';
    tr += '<td align="center" valign="middle" class="tab_grey" ><a href="javascript:;" onclick="deleteATableValidationRight(this);">删除</a></td>';
    tr += '</tr>';
    return tr; 
}

//右边的表达式再增加一行
function addBiaojianJiaoheRight(){
	var tableTr = handleDataBiaojianJiaoheRight();
	$("#biaojianValidationTable").append(tableTr);
}

var allBiaojianValidationTr = "";
//得到该版本下的所有报表，并拼好字符串
function getAllTemplateName(){
	var annualId = $("#annualId").val();
	var biaojianValidationFirstTr = "";
	var biaojianValidationTr = "";
	$.ajax({
		url:'${ctx}/reportDesignController/getAllTemplateInfo/'+annualId,
		type:'POST',
		cache:false,
		success:function(data){
		    $.each(data,function(idx,item){
		    	biaojianValidationTr += "<option value='"+item.templateId+"'>" + item.templateName + "</option>";
			});
		    biaojianValidationFirstTr ="<option value='0'>请选择</option>";
		    allBiaojianValidationTr = biaojianValidationFirstTr + biaojianValidationTr;
		}
	});
}
function deleteATableValidationRight(obj){
	$(obj).closest('tr').remove();
}

//根据表间校核的配置，得到公式左边，公式右边，然后进行转换，生成表间校核公式
function createTableValidationFormula(){
	var leftTableValidationFormulaStr = $("#validationFormulaLeftText").val();
	if(leftTableValidationFormulaStr.indexOf("~") >= 0 && leftTableValidationFormulaStr.indexOf(":") >= 0&& leftTableValidationFormulaStr.indexOf("：") >= 0){
		alert("公式左边不正确");
		return false;
	}
	
	if($("#relationSelectName").val() == 0){
		alert("请选择关系");
		return false;
	}
	var tableLength = $("#biaojianValidationTable tr").length;
	if(tableLength < 2){
		alert("请增加右边公式");
		return false;
	}
	var valudationFormulaSymbol = $("#valudationFormulaSymbol").val();
	
	var templateSelectValueArray = document.getElementsByName("templateSelectName");
	var expressionTextValueArray = document.getElementsByName("expressionTextName");
	var relationSelectName = $("#relationSelectName").val();
	var templateSelectValueStr = "";
	var expressionTextValueStr = "";
	for(var a = 0; a < templateSelectValueArray.length; a++){
		if(templateSelectValueArray[a].value == '0'){
			alert("请选择报表");
			return false;
		}else{
			templateSelectValueStr += templateSelectValueArray[a].value + ",";
		}
	}
	for(var b = 0; b < expressionTextValueArray.length; b++){
		if(expressionTextValueArray[b].value == '' || expressionTextValueArray[b].value == null){
			alert("请填写公式");
			return false;
		}else{
			expressionTextValueStr += expressionTextValueArray[b].value + ",";
		}
	}
	templateSelectValueStr = templateSelectValueStr.substring(0, templateSelectValueStr.length-1);
	expressionTextValueStr = expressionTextValueStr.substring(0, expressionTextValueStr.length-1);
	var templateId = $("#templateId").val();
	
	$.ajax({
		url:"${ctx}/reportTemplateValidationController/getATableValidationFormula/"+templateId,
		type:'POST',
		cache:false,
		data: {leftTableValidationFormulaStr:leftTableValidationFormulaStr,valudationFormulaSymbol:valudationFormulaSymbol,templateSelectValueStr:templateSelectValueStr,expressionTextValueStr:expressionTextValueStr,relationSelectName:relationSelectName},
		success:function(data){
			layer.alert("保存成功",9,function(index){
				var index = parent.layer.getFrameIndex(window.name);
				parent.layer.close(index);
			});
		}
	});
}
</script>
</html>
